---
title: How to Switch Between Different Models
sidebarTitle: Switching Models
---

When working with Agno, you may need to switch between different models. While Agno supports 20+ model providers, switching between different providers can cause compatibility issues. Switching models within the same provider is generally safer and more reliable.

## Recommended Approach

**Safe:** Switch models within the same provider (OpenAI → OpenAI, Google → Google)  
**Risky:** Switch between different providers (OpenAI ↔ Google ↔ Anthropic)

Cross-provider switching is risky because message history between model providers are often not interchangeable, as some require messages that others don't. However, we are actively working to improve interoperability.

## Safe Model Switching

The safest way to switch models is to change the model ID while keeping the same provider:

```python
from uuid import uuid4
from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.models.openai.chat import OpenAIChat

db = SqliteDb(db_file="tmp/agent_sessions.db")

session_id = str(uuid4())
user_id = "user@example.com"

# Create initial agent with expensive model
expensive_agent = Agent(
    model=OpenAIChat(id="gpt-4o"),
    instructions="You are a helpful assistant for technical discussions.",
    db=db,
    add_history_to_context=True,
)

# Have a conversation
expensive_agent.print_response(
    "Explain quantum computing basics", session_id=session_id, user_id=user_id
)
expensive_agent.print_response(
    "What are the main applications?", session_id=session_id, user_id=user_id
)

# Switch to budget model within same provider (safe)
budget_agent = Agent(
    model=OpenAIChat(id="gpt-4o-mini"),
    instructions="You are a helpful assistant for technical discussions.",
    db=db,
    add_history_to_context=True,
)

# Continue conversation - history shared via session_id
budget_agent.print_response(
    "Can you summarize our discussion so far?", session_id=session_id, user_id=user_id
)
```

## Cross-Provider Switching

Switching between providers can cause compatibility issues and is not recommended for production use without thorough testing:

```python
from uuid import uuid4
from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.models.openai.chat import OpenAIChat
from agno.models.google import Gemini

db = SqliteDb(db_file="tmp/cross_provider_demo.db")

session_id = str(uuid4())
user_id = "user@example.com"

# Create OpenAI agent
openai_agent = Agent(
    model=OpenAIChat(id="gpt-4o"),
    instructions="You are a helpful assistant.",
    db=db,
    add_history_to_context=True,
)

# Have a conversation with OpenAI
openai_agent.print_response(
    "Explain machine learning basics", session_id=session_id, user_id=user_id
)
openai_agent.print_response(
    "What about deep learning?", session_id=session_id, user_id=user_id
)

# Now switch to Google Gemini using same session
gemini_agent = Agent(
    model=Gemini(id="gemini-2.0-flash-exp"),
    instructions="You are a helpful assistant.",
    db=db,
    add_history_to_context=True,
)

# This may work but with unpredictable results due to message format differences
gemini_agent.print_response(
    "Continue our discussion about machine learning", session_id=session_id, user_id=user_id
)
# Note: Gemini may not properly interpret OpenAI's message history format
```

## Learn More

- [All supported models](/concepts/models/overview)
- [Environment variables setup](/faq/environment-variables)
